#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<unordered_map>
#include"Log.hpp"

const std::string sep=":";

class Dict
{
private:
    void LoadDict(const std::string path)
    {
        std::ifstream in(path);
        if(!in.is_open())
        {
            LOG(FATAL,"open %s fail\n",path.c_str());
            exit(1);
        }
        std::string line;
        while(std::getline(in,line))
        {
            LOG(DEBUG,"load %s succsee\n",line.c_str());
            if(line.empty())continue;
            auto pos=line.find(sep);
            if(pos==std::string::npos)continue;

            std::string key=line.substr(0,pos);
            if(key.empty())continue;
            std::string val=line.substr(pos+sep.size());
            if(val.empty())continue;

            _dict.insert(std::make_pair(key,val));
        }
        LOG(INFO,"open %s done\n",path.c_str());

        in.close();
    }
public:
    Dict(const std::string path):
    _dict_path(path)
    {
        LoadDict(path);
    }
    std::string TransLate(const std::string &word)
    {
        if(word.empty())return "none";
        auto iter=_dict.find(word);
        if(iter==_dict.end())return "none";
        return iter->second;
    }
    ~Dict()
    {}
private:
    std::unordered_map<std::string,std::string> _dict;
    std::string _dict_path;

};